package gd.qaq.blue.boundary;

import gd.qaq.blue.boundary.command.BoundaryCommands;
import gd.qaq.blue.boundary.dto.Point;
import gd.qaq.blue.boundary.enums.BorderState;
import gd.qaq.blue.boundary.enums.PointType;
import gd.qaq.blue.boundary.listener.BorderListener;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static gd.qaq.blue.boundary.util.PluginHelper.loadData;

public class Boundary extends JavaPlugin {
    public static FileConfiguration config;
    public static Boundary plugin;
    public static Map<String, Map<PointType, Point>> borderMappers = new HashMap<>();


    @Override
    public void onEnable() {
        // Plugin startup logic
        plugin = this;
        config = loadData(plugin);
        initData();
        Bukkit.getPluginCommand("boundary").setExecutor(new BoundaryCommands());
        Bukkit.getPluginManager().registerEvents(new BorderListener(), this);
    }

    public static void initData() {
        ConfigurationSection section = config.getConfigurationSection("worldBorders");
        Set<String> worldNames = section.getValues(false).keySet();
        for (String worldName : worldNames) {
            plugin.getLogger().info("已成功加载世界名为" + worldName + "的边界信息");
            String path = "worldBorders." + worldName + ".";
            Map<PointType, Point> pointMappers = new HashMap<>();
            double yMax = config.getDouble(path + "y.max.value");
            BorderState yMaxType = BorderState.valueOf(config.getString(path + "y.max.type").toUpperCase());
            pointMappers.put(PointType.YMAX, new Point(yMax, yMaxType));
            double yMin = config.getDouble(path + "y.min.value");
            BorderState yMinType = BorderState.valueOf(config.getString(path + "y.min.type").toUpperCase());
            pointMappers.put(PointType.YMIN, new Point(yMin, yMinType));
            double xMax = config.getDouble(path + "x.max.value");
            BorderState xMaxType = BorderState.valueOf(config.getString(path + "x.max.type").toUpperCase());
            pointMappers.put(PointType.XMAX, new Point(xMax, xMaxType));
            double xMin = config.getDouble(path + "x.min.value");
            BorderState xMinType = BorderState.valueOf(config.getString(path + "x.min.type").toUpperCase());
            pointMappers.put(PointType.XMIN, new Point(xMin, xMinType));
            double zMax = config.getDouble(path + "z.max.value");
            BorderState zMaxType = BorderState.valueOf(config.getString(path + "z.max.type").toUpperCase());
            pointMappers.put(PointType.ZMAX, new Point(zMax, zMaxType));
            double zMin = config.getDouble(path + "z.min.value");
            BorderState zMinType = BorderState.valueOf(config.getString(path + "z.min.type").toUpperCase());
            pointMappers.put(PointType.ZMIN, new Point(zMin, zMinType));
            borderMappers.put(worldName, pointMappers);
        }
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
